package cn.cs.controller;

import cn.cs.client.RestFaultTypeClient;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.pojo.ExpensesExplain;
import cn.cs.pojo.FaultType;
import cn.cs.service.impl.ExpensesExplainServiceImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/expensesExplain")
public class ExpensesExplainController {

    @Resource
    private ExpensesExplainServiceImpl expensesExplainService;

    @Resource
    private RestFaultTypeClient faultTypeClient;


    /**
     * 查询资费说明 分页
     *
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getExpensesExplainByPage")
    public Dto getExpensesExplainByPage(Integer pageNumber, Integer pageSize, String areaId, String faultTypeId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("areaId", areaId);
        param.put("faultTypeId", faultTypeId);
        return DtoUtil.returnSuccess("获取成功", expensesExplainService.queryExpensesExplainPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 查询资费说明
     *
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/p/getExpensesExplainByAreaTypeId")
    public Dto getExpensesExplainByAreaTypeId(String areaId, String faultTypeId) throws Exception {
        Map<String, Object> map = expensesExplainService.getAreaById(areaId);
        if(EmptyUtils.isNotEmpty(map)){
            areaId = map.get("area_parent_id").toString();
        }
        Map<String, Object> param = new HashMap<>();
        param.put("areaId", areaId);
        param.put("faultTypeId", faultTypeId);
        List<ExpensesExplain> list = expensesExplainService.getExpensesExplainListByMap(param);
        if(EmptyUtils.isEmpty(list) || list.size() <= 0){
            param.put("areaId", "000000");
            list = expensesExplainService.getExpensesExplainListByMap(param);
        }
        return DtoUtil.returnSuccess("获取成功", list);
    }

    /**
     * 新增/编辑 资费说明
     * @param expensesExplain
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateExpensesExplain")
    public Dto updateExpensesExplain(@RequestBody ExpensesExplain expensesExplain) {
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(expensesExplain.getId())) {
                expensesExplain.setId(IdWorker.getId());
                String faultTypeId = expensesExplain.getFaultTypeId();
                FaultType faultType = faultTypeClient.getFaultTypeByOnlyId(faultTypeId);
                String serviceTypeId = faultType.getParentId();
                expensesExplain.setServiceTypeId(serviceTypeId);
                expensesExplainService.addExpensesExplain(expensesExplain);
            } else {
                expensesExplainService.modifyExpensesExplain(expensesExplain);
                msg = "修改成功";
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除资费说明信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delExpensesExplain")
    public Dto delExpensesExplain(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += expensesExplainService.deleteExpensesExplainById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

}
